#ifndef CVX_EM_TREE_SOLVER_H
#define CVX_EM_TREE_SOLVER_H

#include "cvxImage.h"
#include "cvxAdaptiveQuadTree.h"
#include "cvxIntrinsicSolver.h"

/************************************************************************/
/* 
	CvxEMtreeSolver: global solver with two quadtree
*/
/************************************************************************/

class CvxEMtreeSolver:public CvxIntrinsicSolver
{
public:
	CvxEMtreeSolver();
	~CvxEMtreeSolver();

	// shading map 0.0: unknown area, other is known
	// threshold is deviation of color to divide quadtree
	bool SetETree(const Mat &initialShadingMap, const int threshold = 5);	
	bool SolveETree(bool useEdge);	
	bool CalcMTree(bool reverse);
	bool SolveMtree(bool useEdge);
	// up sample using A vector to get final result
	bool UpsampleAvector();

	bool UpdataETreeEdgeNode();

	bool SetETree(const int stddevThreshold, const int sizeThrehold = 16, const float minimumShadingScale = 1.0);

	// save for visualization purpose
	void SaveQuadTree(const string &fileName, bool isShow);
	void SaveColorQuadTree(const string &fileName, bool isShow);
	void SaveShadingImage(const string &fileName, bool isShow);
	void SaveAlbedoImage(const string &fileName, bool isShow);

	// for test only
	bool SetGroundTruth2EdgeNode(Mat &shadingGroundTruth);
	
//	void SaveColoredShadingImage(const string &fileName);
	
private:
	CvxAdaptiveQuadTree *m_ETree;  //solve unknown area of local solver result
	CvxAdaptiveQuadTree *m_MTree;  //refine known area
};


#endif
